package com.hot100;

//https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/577298/shua-chuan-lc-yan-ge-ologn100yi-qi-kan-q-xifo/?envType=featured-list&envId=2cktkvj%3FenvType%3Dfeatured-list&envId=2cktkvj
public class Solution33 {
    public int search(int[] nums, int target) {
        int i = 0;
        int j = nums.length - 1;
        int mid;
        while (i <= j) {
            mid = (i + j) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] < nums[j]) {
                if (target>nums[mid]&&target<=nums[j]) {
                    i = mid + 1;
                } else {
                    j = mid - 1;
                }
            } else {
                if (target<nums[mid]&&target>=nums[i]) {
                    j = mid - 1;
                } else {
                    i = mid + 1;
                }
            }
        }
        return -1;
    }
}
